package com.creditease.rss.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.http.HttpEntity;

import android.os.SystemClock;
import android.util.Log;

import com.creditease.rss.vo.PicDownloadVo;

public class PicDownloadUtil {
	private static final String TAG = PicDownloadUtil.class.getName();
	private static int _screenWidth;
	private static int _screenHeight;

	private PicDownloadUtil() {
	}

	private static LinkedBlockingQueue<PicDownloadVo> downloadQueue = new LinkedBlockingQueue<PicDownloadVo>();
	private static ExecutorService es = Executors.newFixedThreadPool(5);

	public static void add2Queue(String url, String path) {
		PicDownloadVo vo = new PicDownloadVo(url, path);
		downloadQueue.add(vo);
	}

	public static void init(int screenWidth, int screenHeight) {
		_screenWidth = screenWidth;
		_screenHeight = screenHeight;
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				download();
			}
		});
		thread.start();
	}

	protected static void download() {

		while (true) {
			List<PicDownloadVo> list = getDownloadList(5);
			if (list.isEmpty()) {
				/*try {
					Thread.sleep(5 * 1000);
				} catch (InterruptedException e) {
				}*/
				 SystemClock.sleep(5000);
			} else {
				// Log.d(TAG, "正在下载图片：" + list.size());
				// 下载图片
				for (final PicDownloadVo vo : list) {
					File f = new File(vo.sdpath);
					if (f.exists()) {
						continue;
					}
					es.execute(new Runnable() {
						@Override
						public void run() {
							try {
								download(vo);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					});
				}
				list.clear();
			}
		}
	}

	protected static List<PicDownloadVo> getDownloadList(int size) {
		List<PicDownloadVo> list = new ArrayList<PicDownloadVo>();
		for (int i = 0; i < size; i++) {
			PicDownloadVo vo = downloadQueue.poll();
			if (vo != null) {
				list.add(vo);
			} else {
				break;
			}
		}
		return list;
	}

	protected static void download(PicDownloadVo vo) throws IOException {
		File file = new File(vo.sdpath);
		if (file.exists()) {
			return;
		}
		if (vo != null) {
			Log.d(TAG, "开始下载："+vo.url);
			try {
				InputStream is = HttpUtils.getHttpStream(vo.url);
				
				FileOutputStream fos = new FileOutputStream(file);
				byte buf[]=new byte[4096];
				int len=0;
				while((len=is.read(buf))!=-1){
					fos.write(buf, 0, len);
				}
				fos.flush();
				fos.close();
				is.close();
				
				/*// 缩小图片
				Bitmap bitmap = BitmapFactory.decodeStream(is);
				if (bitmap != null) {
					
					 * Bitmap resizeBitmap = ThumbnailUtils.extractThumbnail(
					 * bitmap, _screenWidth, _screenHeight,
					 * ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
					 
						 * float scaleWidth = (float) _screenWidth /
						 * bitmap.getWidth(); float scaleHeight = (float)
						 * _screenHeight / bitmap.getHeight(); Matrix matrix =
						 * new Matrix(); matrix.postScale(scaleWidth,
						 * scaleHeight); Bitmap resizeBitmap =
						 * Bitmap.createBitmap( bitmap,0,0,
						 * bitmap.getWidth(),bitmap.getHeight(),matrix, false);
						 * bitmap.recycle();
						 
					FileOutputStream fos = new FileOutputStream(file);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
					fos.flush();
					fos.close();
					is.close();
				}*/
				Log.d(TAG, "下载完成："+vo.url+",路径："+vo.sdpath);
			} catch (IOException e) {
				String msg=ExceptionUtil.exception2String(e);
				LogUtil.d(msg);
				Log.w(TAG, "下载异常："+vo.url+",路径："+vo.sdpath);
				Log.w(TAG, msg);
				
			} 
			
		}
	}
}
